package com.senven.branch.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.senven.branch.mapper.BusinessTypeMapper;
import com.senven.branch.service.IBusinessTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senven.common.entity.BusinessType;
import com.senven.common.entity.Permission;
import com.senven.common.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wt
 * @since 2022-09-05
 */
@Service
public class BusinessTypeServiceImpl extends ServiceImpl<BusinessTypeMapper, BusinessType> implements IBusinessTypeService {

    @Autowired
    private BusinessTypeMapper businessTypeMapper;

    @Override
    public CommonResult findAllBusinessType() {
        QueryWrapper<BusinessType> wrapper = new QueryWrapper();
        List<BusinessType> list = businessTypeMapper.selectList(wrapper);

        List<BusinessType> first = new ArrayList<>();
        for (BusinessType xx:list){
            if (xx.getMbtid()==0){
                first.add(xx);
            }
        }
        System.out.println(first);

        //为一级菜单设置二级菜单
        for (BusinessType firs : first) {
            //根据一级菜单id 查询 该菜单得二级菜单。如果出现不确定有几级菜单 那么我们可以使用方法得递归调用
            firs.setChildren(findChildren(list, firs.getBtid()));

        }
        return new CommonResult(2000,"查询成功",first);
    }

    //方法递归
    private List<BusinessType> findChildren(List<BusinessType> permissionList, Integer id) {
        List<BusinessType> children = new ArrayList<>();
        for (BusinessType p : permissionList) {
            if (p.getMbtid().equals(id)) {
                children.add(p);
            }
        }


        for (BusinessType child : children) {
            child.setChildren(findChildren(permissionList, child.getBtid()));
        }

        return children;

    }
}
